package org.djw.user.service.impl;

import org.djw.basic.constant.BaseConstants;
import org.djw.basic.exception.BusinessException;
import org.djw.basic.service.impl.BaseServiceImpl;
import org.djw.basic.util.MD5Utils;
import org.djw.basic.util.StrUtils;
import org.djw.user.domain.Logininfo;
import org.djw.user.domain.User;
import org.djw.user.dto.UserDto;
import org.djw.user.mapper.LogininfoMapper;
import org.djw.user.mapper.UserMapper;
import org.djw.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


/**
 * 用户模块业务层
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public User loadByphone(String phone) {
        return userMapper.loadByphone(phone);
    }

    @Override
    public User loadByemail(String email) {
        return userMapper.loadByemail(email);
    }

    @Override
    public void phoneRegister(UserDto userDto) {
        //空校验
        if(StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPassword())
                || StringUtils.isEmpty(userDto.getVerifyCode())
                ){
            throw new BusinessException("手机号不能为空!!!");
        }
        //密码校验
        if (!userDto.getPassword().equals(userDto.getConfirmPassword())){
            throw new BusinessException("两次输入的密码不同,请重新输入!");
        }
        //用户是否存在校验
        User userTmp = userMapper.loadByphone(userDto.getPhone());
        if (userTmp != null){
            throw new BusinessException("用户已经存在,请重新输入!");
        }
        //校验验证码是否已经失效(超过三分钟)
        Object codeTmp = redisTemplate.opsForValue().get(BaseConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if (codeTmp == null){
            throw new BusinessException("验证码已经过期,请重新获取!");
        }
        //验证验证码是否正确  equalsIgnoreCase不区分大小写进行值比对
        String code = ((String) codeTmp).split(":")[0];
        if (!code.equalsIgnoreCase(userDto.getVerifyCode())){
            throw new BusinessException("验证码错误!");
        }
        //保存数据
        User user = userDto2user(userDto);
        Logininfo logininfo = user2Longininfo(user);
        //保存logininfo并返回自增id
        logininfoMapper.save(logininfo);
        //将返回的id设置给Logininfo_id
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Longininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //拷贝logininfo
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    private User userDto2user(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        //使用盐值加密
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }


}
